<template>
  <Form ref="form" :model="form" :rules="rule" :label-width="100">
    <FormItem label="批次号" prop="lotBatchNo">
      <Input v-model="form.lotBatchNo" placeholder=""></Input>
    </FormItem>
    <FormItem label="批次号" prop="shopAmountList">
      <List border size="small">
        <ListItem v-for="(item,index) in shopList" :key="index">
          <Row style="width:100%;">
            <Col span="8">{{item.district}}</Col>
            <Col span="12">{{item.name}}</Col>
            <Col span="4">
              <Input v-model="form.shopAmountList[index].amount"  placeholder="数量"></Input>
            </Col>
          </Row>
        </ListItem>
      </List>
    </FormItem>
  </Form>
  </template>
<script>
import { save, update, get } from '@/api/wzyy/wzyy.lotbatch.service.js'
import { listByPage as listShopByPage } from '@/api/wzyy/wzyy.shop.service.js'

export default {
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    params: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      shopList: [],
      form: {
        id: 0,
        lotBatchNo: '',
        shopAmountList: []
      },
      rule: {
        lotBatchNo: [
          { required: false, message: '批次号不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    if (this.isEdit) {
      get({
        id: this.params.id
      }).then(res => {
        this.form = this.$util.copy(res.data, this.form)
      })
    }
    listShopByPage({}).then(res => {
      res.data.rows.forEach((item) => {
        this.form.shopAmountList.push({
          shopId: item.id,
          amount: 10
        })
      })
      this.shopList = res.data.rows
    })
  },
  methods: {
    submit () {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            if (this.isEdit) {
              update(this.form).then(res => {
                resolve(res)
              }).catch(e => {
                reject(e)
              })
            } else {
              delete this.form.id
              save(this.form).then(res => {
                resolve(res)
              }).catch(e => {
                reject(e)
              })
            }
          } else {
            reject(new Error('error'))
          }
        })
      })
    }
  }
}
</script>
